(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	var now = 0;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initTime();
			getGradeAttence();
//			initLeave(1,pageSize);
		});
	});
	function initTime(){
		var date = new Date();
		var newDate = getFormatDate(date,"yyyy-MM-dd hh:mm");
		var startTime = getFormatDate(new Date(date.getTime()-1000*60*60*24),"yyyy-MM-dd ");
		$("#start-time").val(startTime+"00:00");
		$("#end-time").val(newDate);
//		$("#start-time").val(getFormatDateByLong(1457193600000,"yyyy-MM-dd hh:mm"));
//		$("#end-time").val(getFormatDateByLong(1457452800000,"yyyy-MM-dd hh:mm"));
	}
	function getGradeAttence(){
		processBegin("数据加载中······");
		$("#st-name").text($(".students-choice-select",parent.document).text());
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var studentcode = $(".students-choice-select",parent.document).attr("studentcode");
		var starttime = strToDate($("#start-time").val() + ":00");
		var endtime = strToDate($("#end-time").val() + ":00");
		var obj = {
				starttime:starttime+"",
				endtime:endtime+"",
				studentcode:studentcode
		};
		var data = {
				cmd:"queryAttendanceForUserWeb",
				model:"attendance",
				userKey:userKey,
				data:obj,
				remark:"",
				
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			if(res.result == 1 && res.data != undefined){
				if(res.data != ""){
					$("#tot-times").text(res.data.list[0].times);
					$("#lea-times").text(res.data.list[0].leavenums);
					$("#ad-times").text(res.data.list[0].absencenums);
					$("#la-times").text(res.data.list[0].latenums);
					$("#ill-times").text(res.data.list[0].illegaloutnums);
					var list = res.data.details;
					if(list.length > 0){
						$("#grade-table").empty();
						$("#grade-table").html(_.template($("#data-template").html(),list));
						processEnd("加载成功");
					}else{
						$("#tot-times").text("0");
						$("#lea-times").text("0");
						$("#ad-times").text("0");
						$("#la-times").text("0");
						$("#ill-times").text("0");
						content += "<div class=\"table-trs\">" +
										"<div class=\"no-data-show\">没有查到相关结果！</div>" +
									"</div>";
						$("#grade-table").empty();
						$("#grade-table").append(content);
						processEnd("加载成功");
					}
				}else{
					$("#tot-times").text("0");
					$("#lea-times").text("0");
					$("#ad-times").text("0");
					$("#la-times").text("0");
					$("#ill-times").text("0");
					content += "<div class=\"table-trs\">" +
					"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#grade-table").empty();
					$("#grade-table").append(content);
					processEnd("加载成功");
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#grade-table").empty();
				$("#grade-table").append(content);
				processError("加载失败");
			}
		},null,null);	
	}
	function initLeave(currentpage,pagesize){
		var userKey = $("#user-name",parent.parent.document).attr("key");		
		var starttime = strToDate($("#start-time").val() + ":00")/1000;
		var endtime = strToDate($("#end-time").val() + ":00")/1000;
		var studentcode = $(".students-choice-select",parent.document).attr("studentcode");
		var name = $(".students-choice-select",parent.document).attr("name");
		var obj = {
				starttime:starttime,
				endtime:endtime,
				studentcode:studentcode,
				currentpage:currentpage,
				pagesize:pagesize
		};
		var data = {
				cmd:"getAttendanceByPage",
				model:"attendance",
				userKey:userKey,
				data:obj,
				remark:"",					
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			var pagecontent = "<div class=\"pages-btn-next\">&gt;&gt;</div>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data.data;
				var total = res.data.total;
				totalSize = Math.ceil(total/pagesize);
				if(totalSize <= 5 && totalSize >0){
					for(var i = totalSize-1 ; i >= 0; i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}else if(totalSize == 0){
					pagecontent += "<div class=\"pages-btn\">1</div>";
				}else if(totalSize > 5){
					var lastp = (now*5)+5;
					if(lastp > totalSize){
						lastp = totalSize;
					}
					for(var i = lastp-1 ; i >= (now*5); i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}
				pagecontent += "<div class=\"pages-btn-last\">&lt;&lt;</div>";
				$("#pattence-page").empty();
				$("#pattence-page").append(pagecontent);
				
				$(".pages-btn").unbind("click");
				$(".pages-btn").click(function(){
					$(".pages-btn").removeClass("page-btn-select");
					$(this).addClass("page-btn-select");
					currentPage = parseInt($(this).text());
					initLeave(currentPage,pageSize);
				});
				
				$(".pages-btn-last").unbind("click");
				$(".pages-btn-last").click(function(){
					if(totalSize <= 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(now > 0){
							now --;
							initLeave((now*5+1),pageSize);
						}else{
							return false;
						}
					}
				});
				
				$(".pages-btn-next").unbind("click");
				$(".pages-btn-next").click(function(){
					if(totalSize <= 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(totalSize%5 == 0){
							if((now+1) < totalSize/5){
								now ++;
								initLeave((now*5+1),pageSize);
							}else{
								return false;
							}
						}else{
							if((now+1) <= (totalSize-totalSize%5)/5){
								now ++;
								initLeave((now*5+1),pageSize);
							}else{
								return false;
							}
						}
					}
				});
				if(data.length > 0){
					for(var i = 0; i < data.length; i++){
						for(var key in data[i]){
							if(data[i][key] == null || data[i][key] == "null" || data[i][key] == undefined){
								data[i][key] = "";
							}
						}
						if(data[i].timetype == 1){
							data[i].timetype = "上午";
						}else if(data[i].timetype == 2){
							data[i].timetype = "下午";
						}
						if(data[i].type == 0){
							data[i].type = "正常";
						}else if(data[i].type == 1){
							data[i].type = "请假";
						}else if(data[i].type == 2){
							data[i].type = "异常";
						}
						var time = getFormatDateByLong(parseInt(data[i].time)*1000,"yyyy-MM-dd hh:mm");
						content += "<div class=\"table-trs\">" +
							"<div class=\"tables-left\" style='margin-left:15px;width:120px;'>" +
								"<div class=\"table-name\" style='width:120px;'>"+ name + "</div>" +
								"<div class=\"table-code\" style='width:160px;'></div>" +
							"</div>" +
							"<div class=\"tables-middle\">" +
								"<div class=\"table-class\" style='width:200px;'>"+time + " " + data[i].timetype+"</div>" +
								"<div class=\"table-code\" style='width:150px;'></div>" +
							"</div>" +
							"<div class=\"tables-right\">" +
								"<div class=\"table-address\">类型：  "+data[i].type+"</div>" +
							"</div>" +
						"</div>";
					}
					$("#pattence-data-show").empty();
					$("#pattence-data-show").append(content);
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#pattence-data-show").empty();
					$("#pattence-data-show").append(content);
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#pattence-data-show").empty();
				$("#pattence-data-show").append(content);
			}
		},null,null);	
	}
	
	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	
	function pagebtns(){
		$("#pattence-search-btn").click(function(){
			now = 0;
//			initLeave(1,pageSize);
			getGradeAttence();
		});
		$(".students-choice",parent.document).unbind("click");
		$(".students-choice",parent.document).click(function(){
			$(".students-choice",parent.document).removeClass("classes-choice-select");
			$(this).addClass("students-choice-select");
			now = 0;
			getGradeAttence();
//			initLeave(1,pageSize);
			
		});	
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();